package org.qloudgen.borg.beans;

import java.io.IOException;
import java.io.Serializable;

import org.apache.ibatis.session.SqlSession;
import org.qloudgen.borg.CommonValues;
import org.qloudgen.borg.metadb.ConfigNames;
import org.qloudgen.borg.utils.RemoteShellExec;
import org.qloudgen.borg.utils.Validation;

public class Cluster implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String clusterID;
	private String clusterName;
	private String clusterType;
	private String clusterDesc;
	private String clusterStatus;
	private String clusterInfo;
	private String createTime;
	private String startTime;
	private String sysUser;
	private String sysUserPassword;
	private String masterIp;
	private String isAvailable;
	private String scriptPath;
	private String dataPath;
	private String jarPath;
	private String localJar;
	private String nodeIp;
	private int maxTasks;
	private String version;
	
	public String getClusterID() {
		return clusterID;
	}
	public Cluster setClusterID(String clusterID) {
		this.clusterID = clusterID;
		return this;
	}
	public String getClusterName() {
		return clusterName;
	}
	public void setClusterName(String clusterName) {
		this.clusterName = clusterName;
	}
	public String getClusterDesc() {
		return clusterDesc;
	}
	public void setClusterDesc(String clusterDesc) {
		this.clusterDesc = clusterDesc;
	}
	public String getClusterStatus() {
		return clusterStatus;
	}
	public Cluster setClusterStatus(String clusterStatus) {
		this.clusterStatus = clusterStatus;
		return this;
	}
	public String getCreateTime() {
		return createTime;
	}
	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}
	public String getStartTime() {
		return startTime;
	}
	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}
	public String getSysUser() {
		return sysUser;
	}
	public void setSysUser(String sysUser) {
		this.sysUser = sysUser;
	}
	public String getSysUserPassword() {
		return sysUserPassword;
	}
	public void setSysUserPassword(String sysUserPassword) {
		this.sysUserPassword = sysUserPassword;
	}
	
	public String checkClusterInfo(boolean isCheckName) {
		
		StringBuffer sb = new StringBuffer();
		String ret = null;
		
		// check cluster name
		if (isCheckName) {
			ret = checkClusterName();
			if (ret != null) sb.append(ret+"\n");
		}
		
		//check master ip
		ret = checkNodeIp(this.getMasterIp());
		if (ret != null) sb.append(ret+"\n");
		
		//check sysUser/sysUserPassword
		ret = checkPassword();
		if (ret != null) sb.append(ret+"\n");
		
		return sb.toString();
	}

	public  String 	checkClusterName() {

		if (this.getClusterName() == null) return null;
		SqlSession session = CommonValues.factory.openSession();
		int count = (Integer)session.selectOne(ConfigNames.SELECT_CLUSTER_COUNT, this);
		session.close();
		if (count > 0){
			return this.getClusterName()+" exists!";
		} else {
			return null;
		}
	}
	
	public  String 	checkPassword() {

		RemoteShellExec remote = new RemoteShellExec(this.getMasterIp(),this.getSysUser(),this.getSysUserPassword());
		try {
			boolean ret = remote.login();
			remote.close();
			
			if (!ret) return CommonValues.INFO_CLUSTER_PASSWORD_INCORRECT ;
			
			return null;
			
		} catch (IOException e) {
			
			return CommonValues.INFO_CLUSTER_PASSWORD_INCORRECT ;
		}
		
	}
	
	public static String checkNodeIp(String nodeIp){
			
		if (!Validation.ipIsValid(nodeIp)){
			return "node ip : " + nodeIp + CommonValues.ERROR_IP_FORMAT;
		}
		if (!Validation.succeedsPing(nodeIp)){
			return "node ip : " +nodeIp +CommonValues.ERROR_IP_PING;
		}
		return null;
		
	}

	public String getMasterIp() {
		return masterIp;
	}
	public void setMasterIp(String masterIp) {
		this.masterIp = masterIp;
	}
	public String getClusterType() {
		return clusterType;
	}
	public Cluster setClusterType(String clusterType) {
		this.clusterType = clusterType;
		return this;
	}
	public String getIsAvailable() {
		return isAvailable;
	}
	public void setIsAvailable(String isAvailable) {
		this.isAvailable = isAvailable;
	}
	public String getClusterInfo() {
		return clusterInfo;
	}
	public void setClusterInfo(String clusterInfo) {
		this.clusterInfo = clusterInfo;
	}
	public String getScriptPath() {
		return scriptPath;
	}
	public Cluster setScriptPath(String scriptPath) {
		this.scriptPath = scriptPath;
		return this;
	}
	public String getDataPath() {
		return dataPath;
	}
	public Cluster setDataPath(String dataPath) {
		this.dataPath = dataPath;
		return this;
	}
	/**
	 * @return the maxTasks
	 */
	public int getMaxTasks() {
		return maxTasks;
	}
	/**
	 * @param maxTasks the maxTasks to set
	 */
	public Cluster setMaxTasks(int maxTasks) {
		this.maxTasks = maxTasks;
		return this;
	}
	public String getJarPath() {
		return jarPath;
	}
	public Cluster setJarPath(String jarPath) {
		this.jarPath = jarPath;
		return this;
	}
	public String getLocalJar() {
		return localJar;
	}
	public Cluster setLocalJar(String localJar) {
		this.localJar = localJar;
		return this;
	}
	public String getNodeIp() {
		return nodeIp;
	}
	public Cluster setNodeIp(String nodeIp) {
		this.nodeIp = nodeIp;
		return this;
	}
	/**
	 * @return the version
	 */
	public String getVersion() {
		return version;
	}
	/**
	 * @param version the version to set
	 */
	public void setVersion(String version) {
		this.version = version;
	}
	
}
